package gt.edu.usac.viewcontroller.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import java.sql.Connection;
import java.sql.SQLException;

import java.util.HashMap;

import javax.naming.Context;
import javax.naming.InitialContext;

import javax.servlet.*;
import javax.servlet.http.*;

import javax.sql.DataSource;

import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;

public class printCoordinacion extends HttpServlet {
    private static final String CONTENT_TYPE = "text/html; charset=windows-1252";
    private static final String HTML_CONTENT_TYPE = "text/html; charset=windows-1252";
    private static final String PDF_CONTENT_TYPE = "application/pdf";
    
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }

    public void doGet(HttpServletRequest request, 
                      HttpServletResponse response) throws ServletException, IOException {
            String pseccion = "";
            String ptipocurso = "";
            String ptemporada = "";
            String pnivel = "";
            String pidioma = "";
            String panio = "";
            String reporte = request.getParameter("reporte");
            if(reporte.equals("acta")){
                pseccion = request.getParameter("pseccion");
                ptipocurso = request.getParameter("ptipocurso");
                ptemporada = request.getParameter("ptemporada");
                pnivel = request.getParameter("pnivel");
                pidioma = request.getParameter("pidioma");
                panio = request.getParameter("panio");
            }
        //String departamento = request.getParameter("departamento");
        //listad_cursos
        Connection conn = null;
        try {
                Context initContext = new InitialContext();
                DataSource dataSource = (DataSource) initContext.lookup("jdbc/calusacDS");
                conn = dataSource.getConnection(); 
                HashMap params = new HashMap();
                //params.put("pidPersona","27");
                JasperPrint jasperprint = null;
                if(request.getParameter("reporte").equals("acta")){
                    params.put("pseccion",pseccion);
                    params.put("ptipocurso",ptipocurso);
                    params.put("ptemporada",ptemporada);
                    params.put("pnivel",pnivel);
                    params.put("pidioma",pidioma);
                    params.put("panio",panio);
                    jasperprint = JasperFillManager.fillReport(getServletContext().getRealPath("/") + "acta.jasper", params,conn);
                }else{
                    jasperprint = JasperFillManager.fillReport(getServletContext().getRealPath("/") + "esperados.jasper", params,conn);
                }
                byte[] pdfasbytes = JasperExportManager.exportReportToPdf(jasperprint);
                ServletOutputStream outstream = response.getOutputStream();
                response.setContentType(PDF_CONTENT_TYPE);
                response.setContentLength(pdfasbytes.length);
                response.setHeader("Content-disposition", "inline; filename=\"calusac-constancia.pdf\"");
                outstream.write(pdfasbytes);
        } catch (Exception ex) {
                ex.printStackTrace();
                response.setContentType(HTML_CONTENT_TYPE);
                PrintWriter out = response.getWriter();
                out.println("<html>");
                out.println("<head><title>Preasignado</title></head>");
                out.println("<body>");
                out.println("<p>"+ex.getMessage()+"</p>");
                out.println("</body></html>");
                out.close();
        } finally {
                try {
                        conn.close();
                }catch (SQLException e) { }
        }
    }
}
